using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CallerID.Helpers;

namespace CallerID
{
    public partial class FormPhoneBook: Form
    {
        private Configuration cfg = Configuration.Instance;
        private string insertNumber = string.Empty;

        public FormPhoneBook(string insertNumber)
        {
            InitializeComponent();
            this.insertNumber = insertNumber;
            if(!string.IsNullOrEmpty(this.insertNumber) && cfg.GetPhoneBookItem(this.insertNumber) == null) {
                List<PhoneBookItem> items = new List<PhoneBookItem>();
                items.AddRange(cfg.PhoneBook);
                items.Add(new PhoneBookItem(this.insertNumber, "", ""));
                phoneBookEditorControl.Items = items.ToArray();
            } else {
                phoneBookEditorControl.Items = cfg.PhoneBook;
            }
            this.Shown += new EventHandler(FormPhoneBook_Shown);
            FormHelpers.SetupWindowPlacement(this);            
        }

        public FormPhoneBook()
            : this(string.Empty)
        {
        }

        private void FormPhoneBook_Shown(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.insertNumber)) {
                phoneBookEditorControl.SelectPhoneNumber(this.insertNumber);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            cfg.PhoneBook = phoneBookEditorControl.Items;
            this.DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}